/**
 * 最优优惠券使用组合算法
 * 
 * 题目描述：
3 张满100减10 可以叠加 
2 张 9.2折扣券，每次只能用一张
5张 直接减5元

输入
3 2 5
3
100
200
400

输出
65 6
155 7
388 4


200 
1. 92折扣+优惠10元=174
2.优惠20+92折扣=165

100 10
100 9.2

折扣+直减
100*9.2=92
92-25=67


折扣+优惠
100*0.92=92
92-0（92<100）=92

优惠+直减
100-10=90
90-25=65

优惠+折扣
100-10=90
90*0.92=82.8



解题思路：
1.罗列四种情况：然后对比最后价格，取最优的
2.
 */





class Discount{
    constructor(price, fullDecreaseCount,discountRateCount,directDecreaseCount) {
        this.price = price;
        this.fullDecrease=10;
        this.fullDecreaseCount=fullDecreaseCount;
        this.discountRate=0.92;
        this.discountRateCount=discountRateCount;
        this.directDecrease=5;
            this.directDecreaseCount=directDecreaseCount;
            
            //初始化
            this.reset();

         
      }
      // 使用折扣券
      useDicountRate(){
      
        if(this.discountRateRemain>0){
            this.currentPrice= Math.floor(this.currentPrice*this.discountRate);
            this.discountRateRemain--;
        }
        
      }
    //   // 使用满则减券
    //   useFullDecrease1(){
    //         // 计算能满减几次
    //         let maxCount1=Math.floor(this.currentPrice/100);

    //         while(this.currentPrice>0 && maxCount1>0 && this.fullDecreaseRemain>0){
    //                 this.currentPrice-=this.fullDecrease;
    //                 this.fullDecreaseRemain--;
    //                 maxCount1--
    //         }
    //   }

       // 使用满则减券
       useFullDecrease(){
        // 计算能满减几次,不满就是0
        let canCount=Math.floor(this.currentPrice/100);
        // 获取实际能用多少次
        let useCount= Math.min(canCount,this.fullDecreaseRemain);
            if(useCount>0){
                this.currentPrice-=useCount*this.fullDecrease;
                this.fullDecreaseRemain-=useCount
            }

        }

      // 使用无门槛减券
      useDirectDecrease(){
        // 想使用的张数
        const wantUse=Math.ceil(this.currentPrice/this.directDecrease)
        // 实际能使用的张数
        const canUse=Math.min(wantUse,this.directDecreaseRemain)
        this.currentPrice-=canUse*this.directDecrease;
        if(this.currentPrice<0)this.currentPrice=0;
        this.fullDecreaseRemain-=canUse;
      }
      // 获取当前使用情况
      getCurrentUse(name=''){
        return {price:this.currentPrice,count:this.getTotalUsed(),name:name}
      }
        
      getTotalUsed(){
        return this.fullDecreaseCount-this.fullDecreaseRemain+
                this.discountRateCount-this.discountRateRemain+
                this.directDecreaseCount-this.directDecreaseRemain
      }

      calc1(){
        this.reset();
        // 折扣+直减
        this.useDicountRate();
        this.useDirectDecrease();
        return this.getCurrentUse('折扣+直减');
    }
    calc2(){
        this.reset();
         //折扣+满减
        this.useDicountRate();
        this.useFullDecrease();
        return this.getCurrentUse('折扣+满减');
    }
    calc3(){
        this.reset();
        //满减+直减
        this.useFullDecrease();
        this.useDirectDecrease();
        return this.getCurrentUse('满减+直减');
    }
    calc4(){
        this.reset();
        //满减+折扣
        this.useFullDecrease();
        this.useDicountRate();
        return this.getCurrentUse('满减+折扣');
    }
    reset(){
        this.currentPrice=this.price;
       this.fullDecreaseRemain=this.fullDecreaseCount;
       this.discountRateRemain=this.discountRateCount;
       this.directDecreaseRemain=this.directDecreaseCount;
    }
    //
    getCountInfo(){
        return {
        fullDecreaseCount:this.fullDecreaseCount,
        fullDecreaseRemain:this.fullDecreaseCount,
                
            discountRateCount:this.discountRateCount,
            discountRateRemain:this.discountRateCount,
              
            directDecreaseCount:this.directDecreaseCount,
            directDecreaseRemain:this.directDecreaseCount,
        }
    }

      // 获取最佳优惠组合
      getBestDiscountCombo(arr){
        arr= [this.calc1(),this.calc2(),this.calc3(),this.calc4()]
        //console.log(arr)
        let res={}
        if(arr.length){
            res=arr[0];
            for (let i=1;i<arr.length;i++) {
                if(arr[i].price<res.price){
                    res=arr[i];
                }
            }
           
        }
        console.log(res.price,res.count,res.name)
        return res
      }

      
}

function input(){

    let a=[3,2,5]
    let b=[92,100,200,400,800]
   for (let i = 0; i < b.length; i++) {
        let d=new Discount( b[i],...a)
        d.getBestDiscountCombo()
   }
}

input();


